﻿Imports System.Drawing.Imaging

Public Class OverlayUI
    Public Event ScreenCaptured(ByVal e As ImageCapturedEventArguments)
    Private _Filename As String
    Private _StartingPoint, _FinishingPoint As Point
    Private _IsDrawing As Boolean = False
    Private _Draw As Panel = Nothing
    Private _CurrentMode As ConfigSettings.CaptureType = ConfigSettings.CaptureType.Rectangle
    Public Sub New(ByVal CaptureType As ConfigSettings.CaptureType)
        Me.InitializeComponent()
        Me._CurrentMode = CaptureType
    End Sub
    Private Sub CaptureSelectedWindow(ByVal mousePos As Point)
        mousePos = PointToScreen(mousePos)
        Me._Filename = FileManagement.GetTempFileName
        Dim selectedWindowHandler As IntPtr
        Dim rect As New NativeMethods.RECT
        Try
            selectedWindowHandler = NativeMethods.WindowFromPoint(mousePos)
            NativeMethods.GetWindowRect(selectedWindowHandler, rect)
            Dim region As Rectangle = Utilities.GetRectangle(New Point(rect.Left, rect.Top), New Point(rect.Right, rect.Bottom))
            Using bmp As New Bitmap(region.Width, region.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
                Using bitmapGraphics As Graphics = Graphics.FromImage(bmp)
                    bitmapGraphics.CopyFromScreen(region.Left, region.Top, 0, 0, region.Size)
                    bmp.Save(Me._Filename, ImageFormat.Bmp)
                End Using
            End Using
            Me.onScreenCaptured(_Filename)
        Catch ex As Exception

        End Try
    End Sub
    Private Sub OverlayUI_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        Select Case Me._CurrentMode
            Case ConfigSettings.CaptureType.SelectedWindow
                Me.CaptureSelectedWindow(e.Location)
            Case ConfigSettings.CaptureType.Rectangle
                Me._IsDrawing = True
                Me._StartingPoint = e.Location
                Me.Cursor = Cursors.Cross
        End Select
    End Sub

    Private Sub OverlayUI_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
        If Me._IsDrawing Then
            Me._FinishingPoint = e.Location
            Me._IsDrawing = False
            Me.Cursor = Cursors.Default
            DrawRect()
            Me.SaveImage()
        End If
    End Sub

    Private Sub DrawRect()
        Me._Draw = New Panel
        Me._StartingPoint = PointToScreen(Me._StartingPoint)
        Me._FinishingPoint = PointToScreen(Me._FinishingPoint)

        Dim CurrentPoint As New Point

        If Me._StartingPoint.X < Me._FinishingPoint.X Then
            CurrentPoint.X = Me._StartingPoint.X
            Me._Draw.Width = Me._FinishingPoint.X - Me._StartingPoint.X
        Else
            CurrentPoint.X = Me._FinishingPoint.X
            Me._Draw.Width = Me._StartingPoint.X - Me._FinishingPoint.X
        End If

        If Me._StartingPoint.Y < Me._FinishingPoint.Y Then
            CurrentPoint.Y = Me._StartingPoint.Y
            Me._Draw.Height = Me._FinishingPoint.Y - Me._StartingPoint.Y
        Else
            CurrentPoint.Y = Me._FinishingPoint.Y
            Me._Draw.Height = Me._StartingPoint.Y - Me._FinishingPoint.Y
        End If

        Me._Draw.Location = CurrentPoint

        Me._Draw.BackColor = Color.Red
        Me.Controls.Add(Me._Draw)
    End Sub

    Private Sub SaveImage()
        If Me._Draw IsNot Nothing Then
            Try
                Me._Filename = FileManagement.GetTempFileName
                Using bmp As New Bitmap(Me._Draw.Width, Me._Draw.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
                    Using bitmapGraphics As Graphics = Graphics.FromImage(bmp)
                        bitmapGraphics.CopyFromScreen(PointToScreen(Me._Draw.Location), New Point(0, 0), Me._Draw.Size, CopyPixelOperation.SourceCopy)
                        bmp.Save(_Filename, ImageFormat.Bmp)
                    End Using
                End Using
                Me.onScreenCaptured(_Filename)
            Catch ex As Exception
                'Do nothing
            End Try
        End If
    End Sub

    Private Sub OverlayUI_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.onScreenCaptured(String.Empty)
        End If
    End Sub

    Private Sub onScreenCaptured(ByVal filename As String)
        RaiseEvent ScreenCaptured(New ImageCapturedEventArguments(filename))
        Me.Close()
    End Sub
End Class